import FireData from "../data/FireData";
import Game from "../Game";
import GlobalData from "../../db/GlobalData";
import { LayaPoolName } from "../LayaPoolName";
import BulletScript from "../../component/BulletScript";
import BulletVO from "../../db/vo/BulletVO";
import BulletData from "../data/BulletData";
import { GameCollisionGroup } from "../GameCollisonGroup";



export default class HeroBullet {

    private _fireDatas: FireData[] = [];
    private _muzzleRecoverTime: number = 60 * 1.5;
    private _projectileRecoverTime: number = 60 * 5;
    private _hitRecoverTime: number = 60 * 1.5;

    private _maxBulletLenth = 5;



    public get fireDatas() {
        return this._fireDatas;
    }


    public init(weapon: Laya.Sprite3D) {
        // console.log("HeroBullet init")
        this._fireDatas=[];
        for (let i = 0; i < weapon.numChildren; i++) {
            let obj = weapon.getChildAt(i) as Laya.Sprite3D;
            let fireData: FireData = new FireData();
            fireData.firePos = obj;
            let bulletVO: BulletVO = GlobalData.getData(GlobalData.BulletVO, Number(obj.name));
            let bulletData: BulletData = new BulletData();
            bulletData.attackRate=bulletVO.attackRate;
            bulletData.id = bulletVO.id;
            bulletData.index = i;
            bulletData.attack = Math.round(Game.hero.playerData.attack * bulletVO.attackRate);
            fireData.bullet = bulletData;
            this._fireDatas.push(fireData);
        }
    }

    public updateState(){
        this._fireDatas.map((fireData)=>{
            fireData.bullet.attack= Math.round(Game.hero.playerData.attack * fireData.bullet.attackRate);
        });
      
    }



    public cacheBullet() {
        // console.log("HeroBullet initBullet",this._fireDatas)
        for (let i = 0; i < this._fireDatas.length; i++) {
            // console.log(this._fireDatas[i])
            let bullet = Laya.Sprite3D.instantiate(Laya.loader.getRes(this._fireDatas[i].bulletRes));
            this._fireDatas[i].bulletSp = bullet;
            Game.monsterResClones.push(bullet);
        }
    }

    public getHit(i): Laya.ShuriKenParticle3D {
        let hit: Laya.ShuriKenParticle3D = Laya.Pool.getItem(LayaPoolName.BULLET_HIT + i);
        if (hit == null) {
            hit = Laya.Sprite3D.instantiate(this._fireDatas[i].bulletSp.getChildByName("hit") as Laya.ShuriKenParticle3D) as Laya.ShuriKenParticle3D;
            Game.monsterResClones.push(hit);
            hit.frameOnce(this._hitRecoverTime, hit, function (index) {
                Laya.Pool.recover(LayaPoolName.BULLET_HIT + index, hit);
            }, [i])
        }
        return hit;
    }



    getProjectile(i) {
        let projectile: Laya.Sprite3D = Laya.Pool.getItem(LayaPoolName.BULLET_PROJECTILE + i);
        if (projectile == null) {
            projectile = Laya.Sprite3D.instantiate(this._fireDatas[i].bulletSp.getChildByName("projectile") as Laya.Sprite3D);
            Game.monsterResClones.push(projectile);
            (projectile.addComponent(BulletScript) as BulletScript).bulletData = this._fireDatas[i].bullet;
            // let rgbd: Laya.Rigidbody3D = projectile.getComponent(Laya.Rigidbody3D);
            // rgbd.collisionGroup = GameCollisionGroup.BULLET;
            // rgbd.canCollideWith = GameCollisionGroup.MONSTER;
            // projectile.addComponent(BulletScript);
            // console.log("create.projectile!!!!")
        }
        projectile.frameOnce(this._projectileRecoverTime, projectile, function (index) {

            // (projectile.getComponent(BulletScript) as BulletScript).stop();
            // Laya.Pool.recover(LayaPoolName.BULLET_PROJECTILE + index, projectile);
        }, [i])
        return projectile;
    }

    getMuzzle(i) {
        let muzzle: Laya.ShuriKenParticle3D = Laya.Pool.getItem(LayaPoolName.BULLET_MUZZLE + i);
        if (muzzle == null) {
            muzzle = Laya.Sprite3D.instantiate(this._fireDatas[i].bulletSp.getChildByName("muzzle") as Laya.ShuriKenParticle3D) as Laya.ShuriKenParticle3D;
            Game.monsterResClones.push(muzzle);
            //   console.log("create.muzzle!!!!")
        }
        muzzle.frameOnce(this._muzzleRecoverTime, muzzle, function (index) {
            Laya.Pool.recover(LayaPoolName.BULLET_MUZZLE + index, muzzle);
        }, [i])
        return muzzle;
    }

    clearPool() {
        for (let i = 0; i < this._maxBulletLenth; i++) {
            Laya.Pool.clearBySign(LayaPoolName.BULLET_MUZZLE + i);
            Laya.Pool.clearBySign(LayaPoolName.BULLET_PROJECTILE + i);
            Laya.Pool.clearBySign(LayaPoolName.BULLET_HIT + i);
        }
    }

}
